
function jsSelectItemByValue(objSelect,objItemText) {  
        for(var i=0;i<objSelect.options.length;i++) {  
            if(objSelect.options[i].value == objItemText) {  
                objSelect.options[i].selected = true;  
                break;  
            }  
        }  
    }  

function addItems(objSelect,url,hasSelect,language) {
    $.ajax({
    	async : false,
        url: url,    //后台webservice里的方法名称
        type: "post",
        dataType: "json",
        contentType: "application/json",
        traditional: true,
        success: function (data) {
    		var optionstring = "";
            for (var i in data) {
                var jsonObj =data[i];
                optionstring += "<option value=\"" + jsonObj.key + "\" >" + jsonObj.value + "</option>";
            }
            if(hasSelect){
            	if(language == 'en') {
            		objSelect.html("<option value='' class='text-left'>--Please Select--</option> "+optionstring);
            	}else{
            		objSelect.html("<option value='' class='text-left'>--请选择--</option> "+optionstring);
            	}
            	
            }else{
            	objSelect.html("<option value='' class='text-left'></option> "+optionstring);
            }
        },
        error: function (msg) {
            alert(objSelect.id + "select error");
        }
    });          
}
function addItemsWithPlaceholder(objSelect,url,hasSelect,placeholder) {
    $.ajax({
    	async : false,
        url: url,    //后台webservice里的方法名称
        type: "post",
        dataType: "json",
        contentType: "application/json",
        traditional: true,
        success: function (data) {
    		var optionstring = "";
            for (var i in data) {
                var jsonObj =data[i];
                optionstring += "<option value=\"" + jsonObj.key + "\" >" + jsonObj.value + "</option>";
            }
            if(hasSelect){
            	objSelect.html("<option value='' class='text-left'>--"+placeholder+"--</option> "+optionstring);
            }else{
            	objSelect.html("<option value='' class='text-left'></option> "+optionstring);
            }
        },
        error: function (msg) {
            alert(objSelect.id + "select error");
        }
    });          
}
function addItemsWithPlaceholder(objSelect,url,hasSelect,placeholder) {
    $.ajax({
    	async : false,
        url: url,    //后台webservice里的方法名称
        type: "post",
        dataType: "json",
        contentType: "application/json",
        traditional: true,
        success: function (data) {
    		var optionstring = "";
            for (var i in data) {
                var jsonObj =data[i];
                optionstring += "<option value=\"" + jsonObj.key + "\" >" + jsonObj.value + "</option>";
            }
            if(hasSelect){
            	objSelect.html("<option value='' class='text-left'>--"+placeholder+"--</option> "+optionstring);
            }else{
            	objSelect.html("<option value='' class='text-left'></option> "+optionstring);
            }
        },
        error: function (msg) {
            alert(objSelect.id + "select error");
        }
    });      
}

/**
 * 从后台查询数据填充 select2
 * @param control 控件ID 多个使用，分隔
 * @param url 指定来源url
 * @param defaultVal 默认值，默认是 "--请选择--"
 * @param width 宽度 默认是 "100%"
 * @param async 是否异步 默认是 true
 * @param placeholder
 */
function select2InitForData(controls,url,defaultVal,width,async,placeholder) {
	if(width == null){
		width =  '100%';
	}
	if(async == null){
	    async = true;
    }
    if(placeholder == null){
        placeholder = "--请选择--";
    }
	$.ajax({
		url: url,
        async:async,
	 	dataType: 'json',
		success: function (resultList) {
            var controlArray = controls.split(",");
            controlArray.forEach(function(ele){
                var control = $(ele);
                if (control.hasClass("select2-hidden-accessible")) {
                    control.empty();
                }
                control.select2({
                    placeholder: placeholder,
                    allowClear: true,
                    data:resultList,
                    zIndex:9999,
                    width: width
                });
                control.val(defaultVal).trigger('change');
            })
	    },
	});
}

//control控件，dictionaryCode数据字典code，defaultVal默认值
function select2InitForDic(controls,dictionaryCode,defaultVal,width,async,placeholder) {
    if(width == null){
        width =  '100%';
    }
    if(async == null){
        async = true;
    }
    if(placeholder == null){
        placeholder = "--请选择--";
    }
	$.ajax({
		url: window.location.origin+"/select2/getSelectDictItem.do",
        async:false,
	 	dataType: 'json',
	 	data:{dictionaryCode:dictionaryCode},
		success: function (resultList) {
            var controlArray = controls.split(",");
            controlArray.forEach(function(ele){
                var control = $(ele);
                if (control.hasClass("select2-hidden-accessible")) {
                    control.empty();
                }
                control.select2({
                    placeholder: placeholder,
                    allowClear: true,
                    data:resultList,
                    zIndex:9999,
                    width: width
                });
                control.val(defaultVal).trigger('change');
            })
		},
	});
}

function select2InitialForCNameWorkcode(controls,placeholder,moduleFlag,defaultVal){
    if(placeholder == "" || placeholder == null){
        placeholder = "人员姓名/首字母"
    }
    var controlArray = controls.split(",");
    controlArray.forEach(function(ele){
        var control = $(ele);
        if (control.hasClass("select2-hidden-accessible")) {
            control.empty();
        }
        control.select2({
            minimumInputLength: 2,
            placeholder: placeholder,
            width: '150',
            allowClear: true,
            language: {
                inputTooShort: function (e) {
                    var t = e.minimum - e.input.length;
                    return "请再输入至少" + t + "个字符";
                }
            },
            ajax: {
                url: '/select2/getStaff.do',
                dataType: 'json',
                method: 'post',
                data: function (params) {
                    var keyWord = params.term;
                    var query = {
                        key: params.term,
                        moduleFlag: moduleFlag
                    }
                    if (keyWord.length > 1) {
                        return query;
                    } else {
                        return false;
                    }
                },
                processResults: function (data) {
                    // Tranforms the top-level key of the response object from 'items' to 'results'
                    if(defaultVal!=null){
                        data = defaultVal;
                    }
                    return {
                        results: data
                    };
                }
            }
        });
        // control.val(defaultVal).trigger('change');    //默认值
    })
}
function select2InitialForCNamePCode(controls,placeholder,moduleFlag,defaultVal){
    if(placeholder == "" || placeholder == null){
        placeholder = "人员姓名/首字母"
    }
    var controlArray = controls.split(",");
    controlArray.forEach(function(ele){
        var control = $(ele);
        if (control.hasClass("select2-hidden-accessible")) {
            control.empty();
        }
        control.select2({
            minimumInputLength: 2,
            placeholder: placeholder,
            width: '150',
            allowClear: true,
            language: {
                inputTooShort: function (e) {
                    var t = e.minimum - e.input.length;
                    return "请再输入至少" + t + "个字符";
                }
            },
            ajax: {
                url: '/select2/getStaffPCode.do',
                dataType: 'json',
                method: 'post',
                data: function (params) {
                    var keyWord = params.term;
                    var query = {
                        key: params.term,
                        moduleFlag: moduleFlag
                    }
                    if (keyWord.length > 1) {
                        return query;
                    } else {
                        return false;
                    }
                },
                processResults: function (data) {
                    // Tranforms the top-level key of the response object from 'items' to 'results'
                    return {
                        results: data
                    };
                }
            }
        });
        // control.val(defaultVal).trigger('change');    //默认值
    })
}

function select2CommonAsync(control,url,placeholder,defaultVal,paramArr){
    control = $(control);
    if(defaultVal != null){
        $.ajax({
            url: url,
            async:false,
            dataType: 'json',
            data:{defaultVal:defaultVal},
            success: function (data) {
                if(data != null && data.length>0){
                    control.html("<option value=\"" + data[0].id + "\" >" + data[0].text + "</option>");
                }
            }
        })
    }

    if (control.hasClass("select2-hidden-accessible")) {
        control.empty();
    }
    control.select2({
        minimumInputLength: 2,
        placeholder: placeholder,
        width: '150',
        allowClear: true,
        language: {
            inputTooShort: function (e) {
                var t = e.minimum - e.input.length;
                return "请再输入至少" + t + "个字符";
            }
        },
        ajax: {
            url: url,
            dataType: 'json',
            method: 'post',
            data: function (params) {
                //将paramArr拆分传给后台
                var keyWord = params.term;
                var query = {
                    key: params.term,
                }
                if(paramArr!= null && paramArr.length>0){
                    for(var i=0;i<paramArr.length;i++){
                        query = $.extend(query,paramArr[i]);
                    }
                }
                if (keyWord.length > 1) {
                    return query;
                } else {
                    return false;
                }
            },
            processResults: function (data) {
                // Tranforms the top-level key of the response object from 'items' to 'results'
                return {
                    results: data
                };
            }
        }
    });

}